package com.vilen.retrofit2helloworld_1;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.vilen.retrofit2helloworld_1.api.ApiRx2;
import com.vilen.retrofit2helloworld_1.api.ApiRx3;
import com.vilen.retrofit2helloworld_1.domain.ResponseJson;
import com.vilen.retrofit2helloworld_1.domain.ResponseJson3;
import com.vilen.retrofit2helloworld_1.domain.User;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 最简单的结合示例
 */
public class MainRxActivity3 extends AppCompatActivity {

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://36.107.180.200:8080/")
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .addConverterFactory(JacksonConverterFactory.create())
        .build();
    ApiRx3 apiRx3 = retrofit.create(ApiRx3.class);
    Observable<ResponseJson3<User>> observable = apiRx3.getUserList();

    observable.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<ResponseJson3<User>>() {
          @Override public void onCompleted() {

          }

          @Override public void onError(Throwable e) {
            Log.e("CHX", e.getMessage().toString());
          }

          @Override public void onNext(ResponseJson3<User> userResponseJson3) {
            Log.i("CHX",userResponseJson3.getData().toString());
          }
        });
  }
}
